package com.tom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Driver;

public class Order02DAO {
        public static Connection conn = null;

        public static Connection getConnection() {
                if (conn == null) {
                        try {
                                DriverManager.registerDriver(new Driver());
                                conn = DriverManager.getConnection(
                                		"jdbc:mysql://j.snpy.org/j102u07?useUnicode=true&characterEncoding=UTF-8", "j102u07", "j102u07.989");

                        } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                return conn;

        }

        public List<Order02> getAll() {
                List<Order02> order02s = new ArrayList<Order02>();
                try {
                        Statement stmt = getConnection().createStatement();
                        ResultSet rs = stmt.executeQuery("select * from order02");
                        while (rs.next()) {
                                String ord02_num = rs.getString("ord02_num");
                                String ord02_number = rs.getString("ord02_number");
                                String ord02_desc = rs.getString("ord02_desc");
                                String ord02_name = rs.getString("ord02_name");
                                int ord02_price = rs.getInt("ord02_price");
                                int ord02_outday = rs.getInt("ord02_outday");
                                String ord02_outtime = rs.getString("ord02_outtime");
                                String ord02_address = rs.getString("ord02_address");
                                String ord02_memo = rs.getString("ord02_memo");                
                                Order02 order02 = new Order02(ord02_num, ord02_number, ord02_desc, ord02_name, ord02_price, ord02_outday, ord02_outtime, ord02_address, ord02_memo);
                                order02s.add(order02);
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return order02s;
        }

        public int delete(String ord02_num) {
                String sql = "delete from order02 where ord02_num=?";
                int rowCount = 0;
                try {
                        PreparedStatement pstmt = getConnection().prepareStatement(sql);
                        pstmt.setString(1, ord02_num);
                        rowCount = pstmt.executeUpdate();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return rowCount;
        }
        
        public int add(Order02 o){
                String sql = "insert into order02(ord02_num,ord02_number,ord02_desc,ord02_name,ord02_price,ord02_outday,ord02_outtime,ord02_address,ord02_memo) values (?,?,?,?,?,?,?,?,?)";
                int rowCount = 0;
                try {
                        PreparedStatement pstmt = getConnection().prepareStatement(sql);
                        pstmt.setString(1, o.getOrd02_num());
                        pstmt.setString(2, o.getOrd02_number());
                        pstmt.setString(2, o.getOrd02_number());
                        System.out.println("error");
                        pstmt.setString(3, o.getOrd02_desc());
                        pstmt.setString(4, o.getOrd02_name());
                        pstmt.setInt(5, o.getOrd02_price());
                        pstmt.setInt(6, o.getOrd02_outday());
                        pstmt.setString(7, o.getOrd02_outtime());
                        pstmt.setString(8, o.getOrd02_address());
                        pstmt.setString(9, o.getOrd02_memo());
                        rowCount = pstmt.executeUpdate();
                        System.out.println(rowCount);
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return rowCount;
        }
        
        public int update(Order02 o){
                String sql = "update order02 set ord02_number=?,ord02_desc=?,ord02_name=?,ord02_price=?,ord02_outday=?,ord02_outtime=?,ord02_address=?,ord02_memo=?,where ord02_num=?";
                int rowCount = 0;
                try {
                        PreparedStatement pstmt = getConnection().prepareStatement(sql);
                        pstmt.setString(1, o.getOrd02_number());
                        pstmt.setString(2, o.getOrd02_desc());
                        pstmt.setString(3, o.getOrd02_name());
                        pstmt.setInt(4, o.getOrd02_price());
                        pstmt.setInt(5, o.getOrd02_outday());
                        pstmt.setString(6, o.getOrd02_outtime());
                        pstmt.setString(7, o.getOrd02_address());
                        pstmt.setString(8, o.getOrd02_memo());
                        pstmt.setString(9, o.getOrd02_num());
                        rowCount = pstmt.executeUpdate();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return rowCount;
        }

        public Order02 getOrder02ById(String ord02_num ){
                Order02 order02 = null;
                String sql = "select * from order02 where ord02_num=?";
               
                try {
                        PreparedStatement pstmt = getConnection().prepareStatement(sql);
                        pstmt.setString(1, ord02_num);
                        ResultSet rs = pstmt.executeQuery();
                        if (rs.next()){
                                order02 = new Order02(rs.getString("ord02_num")
                                                , rs.getString("ord02_number")
                                                , rs.getString("ord02_desc")
                                                , rs.getString("ord02_name")
                                                , rs.getInt("ord02_price")
                                                , rs.getInt("ord02_outday")
                                                , rs.getString("ord02_outtime")
                                                , rs.getString("ord02_address")
                                                , rs.getString("ord02_memo"));
                                                
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return order02;
        }
  /*      
        public static void main(String[] args){
//              testgetAll();
                Order01DAO dao = new Order01DAO();
                System.out.println(dao.delete("aa123"));
        }
        */

     /*   private static void testgetAll() {
        	Order01DAO dao = new Order01DAO();
                List<Order01> list = dao.getAll();
                System.out.println(list.size());
        }
        */
}
